package com.ruoyi.ldzlsc.service;

import com.ruoyi.ldzlsc.config.TaskStatusWebSocketHandler;
import com.ruoyi.ldzlsc.config.WebSocketMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * WebSocket服务类
 * 
 * @author ldzl
 * @date 2025-01-23
 */
@Service
public class WebSocketService {

    @Autowired
    private TaskStatusWebSocketHandler webSocketHandler;

    /**
     * 推送任务状态更新消息
     * 
     * @param taskId 任务ID
     * @param oldStatus 旧状态
     * @param newStatus 新状态
     * @param message 状态变更消息
     */
    public void pushTaskStatusUpdate(Long taskId, String oldStatus, String newStatus, String message) {
        Map<String, Object> data = new HashMap<>();
        data.put("taskId", taskId);
        data.put("oldStatus", oldStatus);
        data.put("newStatus", newStatus);
        data.put("statusText", getStatusText(newStatus));
        
        WebSocketMessage<Map<String, Object>> wsMessage = new WebSocketMessage<>(
            "task_status_update", 
            message, 
            data
        );
        
        // 广播给所有连接的客户端
        webSocketHandler.broadcastMessage(wsMessage);
        
        System.out.println("推送任务状态更新: 任务ID=" + taskId + ", 状态=" + oldStatus + "->" + newStatus);
    }

    /**
     * 推送质检进度更新消息
     * 
     * @param taskId 任务ID
     * @param qualityCheckId 质检单ID
     * @param progress 质检进度
     * @param message 进度消息
     */
    public void pushQualityCheckProgress(Long taskId, Long qualityCheckId, String progress, String message) {
        Map<String, Object> data = new HashMap<>();
        data.put("taskId", taskId);
        data.put("qualityCheckId", qualityCheckId);
        data.put("progress", progress);
        
        WebSocketMessage<Map<String, Object>> wsMessage = new WebSocketMessage<>(
            "quality_check_progress", 
            message, 
            data
        );
        
        webSocketHandler.broadcastMessage(wsMessage);
        
        System.out.println("推送质检进度更新: 任务ID=" + taskId + ", 质检单ID=" + qualityCheckId + ", 进度=" + progress);
    }

    /**
     * 推送质检完成消息
     * 
     * @param taskId 任务ID
     * @param qualityCheckId 质检单ID
     * @param result 质检结果 (1=合格, 2=不合格)
     * @param message 完成消息
     */
    public void pushQualityCheckComplete(Long taskId, Long qualityCheckId, String result, String message) {
        Map<String, Object> data = new HashMap<>();
        data.put("taskId", taskId);
        data.put("qualityCheckId", qualityCheckId);
        data.put("result", result);
        data.put("resultText", "1".equals(result) ? "合格" : "不合格");
        
        WebSocketMessage<Map<String, Object>> wsMessage = new WebSocketMessage<>(
            "quality_check_complete", 
            message, 
            data
        );
        
        webSocketHandler.broadcastMessage(wsMessage);
        
        System.out.println("推送质检完成: 任务ID=" + taskId + ", 质检单ID=" + qualityCheckId + ", 结果=" + result);
    }

    /**
     * 推送入库完成消息
     * 
     * @param taskId 任务ID
     * @param inboundId 入库单ID
     * @param quantity 入库数量
     * @param message 完成消息
     */
    public void pushInboundComplete(Long taskId, Long inboundId, String quantity, String message) {
        Map<String, Object> data = new HashMap<>();
        data.put("taskId", taskId);
        data.put("inboundId", inboundId);
        data.put("quantity", quantity);
        
        WebSocketMessage<Map<String, Object>> wsMessage = new WebSocketMessage<>(
            "inbound_complete", 
            message, 
            data
        );
        
        webSocketHandler.broadcastMessage(wsMessage);
        
        System.out.println("推送入库完成: 任务ID=" + taskId + ", 入库单ID=" + inboundId + ", 数量=" + quantity);
    }

    /**
     * 推送给指定用户
     * 
     * @param userId 用户ID
     * @param type 消息类型
     * @param message 消息内容
     * @param data 消息数据
     */
    public void pushToUser(String userId, String type, String message, Object data) {
        WebSocketMessage<Object> wsMessage = new WebSocketMessage<>(type, message, data);
        webSocketHandler.sendMessageToUser(userId, wsMessage);
    }

    /**
     * 获取状态文本
     */
    private String getStatusText(String status) {
        switch (status) {
            case "0": return "未开始";
            case "1": return "已派工";
            case "2": return "进行中";
            case "3": return "已完成";
            case "4": return "已暂停";
            case "5": return "待质检";
            default: return "未知状态";
        }
    }

    /**
     * 获取当前WebSocket连接数
     */
    public int getConnectionCount() {
        return webSocketHandler.getConnectionCount();
    }
}
